﻿using System;
using Landpy.CruiseControlNet.FacadeInterface.ConfigurationModule;
using Landpy.CruiseControlNet.FacadeInterface.RepositoryModule;
using Landpy.CruiseControlNet.Infrastructure.Ioc;

namespace Landpy.CruiseControlNet.ConfigurationSidekick.ConfigTreeModule
{
    abstract class UpdateChildrenNode : IUpdateChildrenNode
    {
        protected IProjectRepository ProjectRepository { get; private set; }
        protected IPermissionRepository PermissionRepository { get; private set; }
        protected ITaskRepository TaskRepository { get; private set; }
        protected IPublisherRepository PublisherRepository { get; private set; }
        protected IEmailGroupRepository EmailGroupRepository { get; private set; }

        public IUpdateChildrenNode Successor { set; protected get; }

        protected UpdateChildrenNode()
        {
            this.ProjectRepository = IOC.GetInstance().ApplicationContext["ProjectRepository"] as IProjectRepository;
            this.PermissionRepository = IOC.GetInstance().ApplicationContext["PermissionRepository"] as IPermissionRepository;
            this.TaskRepository = IOC.GetInstance().ApplicationContext["TaskRepository"] as ITaskRepository;
            this.PublisherRepository = IOC.GetInstance().ApplicationContext["PublisherRepository"] as IPublisherRepository;
            this.EmailGroupRepository = IOC.GetInstance().ApplicationContext["EmailGroupRepository"] as IEmailGroupRepository;
        }

        public abstract void UpdateChildrenNodes(ChildrenNodeContext childrenNodeContext);

        protected TreeNodesBuilder GetTreeNodesBuilder(CruiseControl cruiseControl)
        {
            return new TreeNodesBuilder(cruiseControl);
        }

        protected TreeNodeIndentity GetTreeNodeIndentity(ChildrenNodeContext childrenNodeContext)
        {
            var treeNodeIndentity = new TreeNodeIndentity(TreeNodeType.Unknow, String.Empty);
            if (childrenNodeContext.SelectedNode.Tag != null)
            {
                treeNodeIndentity = (TreeNodeIndentity)childrenNodeContext.SelectedNode.Tag;
            }
            return treeNodeIndentity;
        }
    }
}
